//
//  Player.m
//  PacIsBack
//
//  Created by Simon André on 28/01/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Player.h"
#import "PlayerTeam.h"
#import "PlayerCharacter.h"
#import "Map.h"
#import "CharacterFactory.h"

@implementation Player

@synthesize characters, currentMap, positionX, positionY;

-(id) initPlayer{
	if (self = [super init]) {
		PlayerCharacter* firstCharacter = [CharacterFactory createCharacterWithType:@"Madakia"];
		NSMutableArray* team = [[NSMutableArray alloc] init];
		[team addObject:firstCharacter];
		[self setCharacters:team];
		[team release];
		Map* firstMap = [[Map alloc] initWithDefaultValues];
		[self setCurrentMap:firstMap];
		[firstMap release];
		[self setPositionX:1];
		[self setPositionY:1];
	}
	
	return self;
}

- (PlayerTeam*) battleTeam{
	NSMutableArray* charactersInBattleTeam = [[NSMutableArray alloc] init];
	while (([charactersInBattleTeam count] <= 4) && ([charactersInBattleTeam count]< [[self characters] count])) {
		[charactersInBattleTeam addObject:[[self characters] objectAtIndex:[charactersInBattleTeam count]]];
	}
	PlayerTeam* playerTeam = [[PlayerTeam alloc] initWithCharacters:(NSArray*) charactersInBattleTeam];
	[playerTeam autorelease];
	[charactersInBattleTeam release];
	return playerTeam;
}


- (id<AreaTypeProtocol>) currentArea{
	return [[self currentMap] areaTypeAtCoordinateX:[self positionX] andY:[self positionY]];
}

- (void) dealloc{
	[self setCharacters:nil];
	[self setCurrentMap:nil];
	[super dealloc];
}

@end
